package models

import (
	"kop/modules/award/configuration"
	"kop/pb"
	"kop/util/log"
	"kop/util/rand"

	itemC "kop/modules/item/client"
	moneyC "kop/modules/money/client"
	ringC "kop/modules/ring/client"
	roleC "kop/modules/role/client"
)

func GetByAwardID(roleID, awardID int32) *pb.Award {

	var role = roleC.GetRole(roleID)
	var award = RandAward(role, awardID)
	if err := SendAward(role, award); err != nil {
		panic(err)
	}

	return award
}

func SendAward(role *pb.Role, award *pb.Award) error {

	//if award.Exp > 0 {
	//	roleC.AddExp(role, award.Exp)
	//	award.RoleExp = role.Exp
	//}

	if award.Coin > 0 {
		award.Money = moneyC.AddCoin(role.RoleID, award.Coin)
	}

	for _, v := range award.AwardItems {
		v.Item = itemC.AddItem(role.RoleID, v.RawItem.ItemID, v.RawItem.Number)
	}

	if award.RingIDs != nil {

		award.Rings = []*pb.Ring{}
		for _, ringID := range award.RingIDs {
			var ring = ringC.Create(role.RoleID, ringID)
			award.Rings = append(award.Rings, ring)
		}
	}

	return nil
}

// 随机一组奖励
func RandAward(role *pb.Role, awardID int32) *pb.Award {

	var award pb.Award
	var awardConf, err = configuration.GetByAwardID(awardID)
	if err != nil {
		return &award
	}

	for _, bagID := range awardConf.AwardBag {

		bagConf, err := configuration.GetByAwardBagID(bagID)
		if err != nil {
			continue
		}

		if role.Level < bagConf.LevelMin || role.Level > bagConf.LevelMax {
			continue
		}

		if !rand.IsWin(bagConf.Weight) {
			continue
		}

		var contents = make([]rand.Value, 0, len(bagConf.AwardContent))

		for _, contentID := range bagConf.AwardContent {
			contentConf, err := configuration.GetByContentID(contentID)
			if err != nil {
				log.Error("AwardBag.AwardContent error: " + err.Error())
				continue
			}

			contents = append(contents, contentConf)
		}

		switch bagConf.RuleType {
		case configuration.RuleTypeEveryone:
			// 每种都正常随机(bagConf.Count)次
			for i := 0; i < int(bagConf.Count); i++ {
				for _, v := range contents {

					var conf = v.(configuration.ContentConf)
					if rand.IsWin(conf.Weight) {
						setContent(role, &award, conf)
					}
				}
			}

		case configuration.RuleTypeRandOne:
			// 按总体权重随机(bagConf.Count)次
			for i := 0; i < int(bagConf.Count); i++ {
				var conf = rand.WithWeight(contents, 1)[0]
				setContent(role, &award, conf.(configuration.ContentConf))
			}

		case configuration.RuleTypeRemove:
			// 按总体权重随机 随bagConf.Count次不重复结果
			var configs = rand.WithWeight(contents, int(bagConf.Count))
			for _, conf := range configs {
				setContent(role, &award, conf.(configuration.ContentConf))
			}
		}

	}

	return &award
}

func setContent(role *pb.Role, award *pb.Award, contentConf configuration.ContentConf) {

	switch contentConf.AwardType {
	case configuration.AwardTypeCoin:
		// 银币
		award.Coin += rand.Int64(contentConf.CountMin, contentConf.CountMax)

	//case configuration.AwardTypeRoleExp:
	//	// 主角声望
	//	award.Exp += int32(rand.Int64(contentConf.CountMin, contentConf.CountMax))

	case configuration.AwardIncome:
		// 每秒收入的倍数

		if role.Income == 0 {
			role.Income = moneyC.GetIncome(role.RoleID)
		}

		award.Coin += role.Income * rand.Int64(contentConf.CountMin, contentConf.CountMax)

	case configuration.AwardTypeItem:

		if award.AwardItems == nil {
			award.AwardItems = make([]*pb.AwardItem, 0)
		}

		var itemID = contentConf.PropID
		var number = int32(rand.Int64(contentConf.CountMin, contentConf.CountMax))

		var find = false
		for _, rawItem := range award.AwardItems {

			if rawItem.RawItem.ItemID == itemID {

				find = true
				rawItem.RawItem.Number += number
			}
		}

		if !find {
			award.AddRawItem(&pb.RawItem{ItemID: itemID, Number: number})
		}

	case configuration.AwardTypeRing:

		if award.RingIDs == nil {
			award.RingIDs = make([]int32, 0)
		}

		var ringID = contentConf.PropID
		var number = int32(rand.Int64(contentConf.CountMin, contentConf.CountMax))

		for i := int32(0); i < number; i++ {
			award.RingIDs = append(award.RingIDs, ringID)
		}
	}

}
